package top.oauth2.api.demo.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import top.oauth2.api.demo.config.ApplicationContextComponent;
import top.oauth2.api.demo.service.TestPolymorphismService;

/**
 * @author xphu
 * @version 1.0
 * @date 2021/9/15 11:46
 */
@Service("getPolymorphismService")
public class GetPolymorphismService extends TestPolymorphismService {

    @Autowired
    private ApplicationContextComponent applicationContextComponent;

    @Autowired
    private ApplicationContext applicationContext;


    /**
     * 根据类型调用不同的子类
     * @param type
     * @return
     */
    @Override
    public String getName(Integer type) {
        if (type == 1){
            TestPolymorphismService polymorphismService = (TestPolymorphismService)applicationContext.getBean("testPolymorphismService1");
            return polymorphismService.getName(1);
        } else {
            TestPolymorphismService polymorphismService = (TestPolymorphismService)applicationContext.getBean("testPolymorphismService2");
            return polymorphismService.getName(2);

        }
    }
}
